package com.bat.conf.datasource;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Description: 数据源切换 切面
 * @Author: zpf
 * @Date: 2020/1/17 上午10:37
 */
@Component
@Slf4j
@Aspect
@Order(1)
public class DataSourceAspect {

    @Pointcut("@annotation(com.bat.conf.datasource.DataSourceAnnotation)")
    public void pointCut() {

    }

    @Before("pointCut() && @annotation(dataSourceAnnotation)")
    public void doBefore(DataSourceAnnotation dataSourceAnnotation) {
        log.info("选择数据源---" + dataSourceAnnotation.value());
        //指定key
        DynamicDataSourceContextHolder.setDatabaseType(dataSourceAnnotation.value());
    }

    @After("pointCut()")
    public void doAfter() {
        DynamicDataSourceContextHolder.clear();
    }
}
